<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;

/**
 * App\Subtitle
 *
 * @property int $id
 * @property string $raw_name
 * @property string $name
 * @property string $raw_url
 * @property string $url
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereRawName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereRawUrl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Subtitle whereUrl($value)
 * @mixin \Eloquent
 */
class Subtitle extends Model
{


    protected $fillable = ['version', 'url', 'source_url', 'uri', 'name_zh', 'name_en', 'format', 'lang', 'file_name'];

    public static function SpiderCreate(string $source_url, array $data)
    {

        $instance = Subtitle::firstOrNew(compact('source_url'));

        if (empty($instance->uri)) {
            $data['uri'] = self::Fetch_Subtitle($data['url']);
        }
        array_filter($data);
        $instance->fill($data)->save();

        echo \GuzzleHttp\json_encode($instance) . "\n";
        echo "----------------------------------------------------\n";

    }

    static function Fetch_Subtitle(string $url)
    {

        try {
            $ext = pathinfo($url)['extension'];
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            $ext = 'ext_error';
        }
        list($bucket, $bucketMgr) = self::QINIU_BUCKET();
        $key = 'subtitle/' . (microtime(true) * 10000) . '.' . $ext;
        list($ret, $err) = $bucketMgr->fetch($url, $bucket, $key);
        if ($err !== null) {
            Log::error('七牛下载字幕失败:' . $err->message() . $url);
            return null;
        } else {
            return $ret['key'];
        }
    }

    public static function QINIU_BUCKET(): array
    {
        $bucket = config('app.QN_bucketName');
        $accessKey = config('app.QN_accessKey');
        $secretKey = config('app.QN_secretKey');
        $bucketMgr = new BucketManager(new Auth($accessKey, $secretKey));
        return array($bucket, $bucketMgr);
    }

    public function setNameEnAttribute($value)
    {
        $this->attributes['name_en'] = str_replace(['【英名】'], '', $value);
    }

    public function setNameZhAttribute($value)
    {
        $this->attributes['name_zh'] = str_replace(['【中文】'], '', $value);
    }

    public function setLangAttribute($value)
    {
        $this->attributes['lang'] = str_replace(['【語種】'], '', $value);
    }

    public function setFormatAttribute($value)
    {
        $this->attributes['format'] = str_replace(['【格式】'], '', $value);
    }

    public function setVersionAttribute($value)
    {
        $this->attributes['version'] = str_replace(['【版本】'], '', $value);
    }
}



